package middleware

import (
	"context"
	"log"
	"os"

	"github.com/go-redis/redis/v8"
)

const (
	redisAddress  = "REDIS_ADDRESS"
	redisPassword = "REDIS_PASSWORD"
)

type Redis struct {
	rdb *redis.Client
}

func NewRedisClient() Redis {
	rdb := redis.NewClient(&redis.Options{
		Addr:     os.Getenv(redisAddress),
		Password: os.Getenv(redisPassword),
	})
	log.Printf("Start Redis Client, address: %s\n", os.Getenv(redisAddress))
	return Redis{rdb: rdb}
}

func (c Redis) Visit() {
	val, err := c.rdb.Get(context.Background(), "key").Result()
	if err != nil {
		log.Printf("Fail to query redis: %v", err)
	} else {
		log.Printf("Query Redis: '%s'", val)
	}
}

func (c Redis) Close() {
	_ = c.rdb.Close()
}
